package exp5.Part4;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.Socket;
import java.security.*;
import java.util.Scanner;

/**
 * Created by besti on 2018/6/9.
 */
public class SocketClient {
    public static void main(String[] args) throws IOException {
        //1.建立客户端Socket连接，指定服务器位置和端口
        Socket socket = new Socket("192.168.1.243",8800);

        //2.得到socket读写流
        OutputStream outputStream = socket.getOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
        //输入流
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        //3.利用流按照一定的操作，对socket进行读写操作
        System.out.println("请输入你要计算的中缀表达式：");
        Scanner scan = new Scanner(System.in);
        String infx= scan.nextLine();
        MyBC bc = new MyBC();
        String info1 = bc.getAnswer(infx);
        String info2 = "";

        try {

            // 读取对方的DH公钥
            FileInputStream f1=new FileInputStream("Spub.dat");
            ObjectInputStream b1=new ObjectInputStream(f1);
            PublicKey pbk=(PublicKey)b1.readObject( );
            //读取自己的DH私钥
            FileInputStream f2=new FileInputStream("Cpri.dat");
            ObjectInputStream b2=new ObjectInputStream(f2);
            PrivateKey prk=(PrivateKey)b2.readObject( );
            // 执行密钥协定
            KeyAgreement ka=KeyAgreement.getInstance("DH");
            ka.init(prk);
            ka.doPhase(pbk,true);
            //生成共享信息
            byte[ ] sb=ka.generateSecret();
            System.out.print("共享信息：");
            for(int i=0;i<sb.length;i++){
                System.out.print(sb[i]+",");
            }
            SecretKeySpec k1 =new  SecretKeySpec(sb,"DESede");


            FileInputStream f = new FileInputStream("key1.dat");
            ObjectInputStream b = new ObjectInputStream(f);
            Key k = (Key) b.readObject();
            Cipher cp = Cipher.getInstance("DESede");
            cp.init(Cipher.ENCRYPT_MODE, k);
            byte ptext[] = info1.getBytes("UTF8");
            for (int i = 0; i < ptext.length; i++) {
                System.out.print(ptext[i] + ",");
            }
            System.out.println("");
            byte ctext[] = cp.doFinal(ptext);
            for (int i = 0; i < ctext.length; i++) {
                info2 += ctext[i] + ",";
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        System.out.println("已发送");
        String info = new String(info2.getBytes("GBK"),"utf-8");
        outputStreamWriter.write(info);
        outputStreamWriter.flush();
        socket.shutdownOutput();
        //接收服务器的响应
        String reply = null;
        while (!((reply = bufferedReader.readLine()) ==null)){
            System.out.println("接收服务器的结果为：" + reply);
        }
        //4.关闭资源
        bufferedReader.close();
        inputStream.close();
        outputStreamWriter.close();
        outputStream.close();
        socket.close();
    }
}


